package icu.qx.gbs.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.gbs.mapper.GbsGoodFileMapper;
import icu.qx.gbs.domain.GbsGoodFile;
import icu.qx.gbs.service.IGbsGoodFileService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物品附件Service业务层处理
 *
 * @author yong
 * @date 2024-06-30
 */
@Service
public class GbsGoodFileServiceImpl implements IGbsGoodFileService {
    @Autowired
    private GbsGoodFileMapper gbsGoodFileMapper;

    /**
     * 查询物品附件
     *
     * @param fileId 物品附件主键
     * @return 物品附件
     */
    @Override
    public GbsGoodFile selectGbsGoodFileByFileId(Long fileId) {
        return gbsGoodFileMapper.selectGbsGoodFileByFileId(fileId);
    }

    /**
     * 查询物品附件列表
     *
     * @param gbsGoodFile 物品附件
     * @return 物品附件
     */
    @Override
    public List<GbsGoodFile> selectGbsGoodFileList(GbsGoodFile gbsGoodFile) {
        return gbsGoodFileMapper.selectGbsGoodFileList(gbsGoodFile);
    }

    /**
     * 新增物品附件
     *
     * @param gbsGoodFile 物品附件
     * @return 结果
     */
    @Override
    public int insertGbsGoodFile(GbsGoodFile gbsGoodFile) {
        gbsGoodFile.setCreateTime(DateUtils.getNowDate());
        return gbsGoodFileMapper.insertGbsGoodFile(gbsGoodFile);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int insertGbsGoodFiles(List<GbsGoodFile> gbsGoodFiles) {
        GbsGoodFile file = gbsGoodFiles.get(0);
        gbsGoodFileMapper.deleteGbsGoodFileByPdCode(file.getFilePdCode());
        gbsGoodFiles.forEach(gbsGoodFile -> {
            gbsGoodFile.setCreateTime(DateUtils.getNowDate());
            gbsGoodFile.setCreateBy(SecurityUtils.getUsername());
            gbsGoodFileMapper.insertGbsGoodFile(gbsGoodFile);
        });
        return 1;
    }

    /**
     * 修改物品附件
     *
     * @param gbsGoodFile 物品附件
     * @return 结果
     */
    @Override
    public int updateGbsGoodFile(GbsGoodFile gbsGoodFile) {
        gbsGoodFile.setUpdateTime(DateUtils.getNowDate());
        return gbsGoodFileMapper.updateGbsGoodFile(gbsGoodFile);
    }

    /**
     * 批量删除物品附件
     *
     * @param fileIds 需要删除的物品附件主键
     * @return 结果
     */
    @Override
    public int deleteGbsGoodFileByFileIds(Long[] fileIds) {
        return gbsGoodFileMapper.deleteGbsGoodFileByFileIds(fileIds);
    }

    /**
     * 删除物品附件信息
     *
     * @param fileId 物品附件主键
     * @return 结果
     */
    @Override
    public int deleteGbsGoodFileByFileId(Long fileId) {
        return gbsGoodFileMapper.deleteGbsGoodFileByFileId(fileId);
    }
}
